package stu.day33_day38.day35;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

public class hw1 {
    public int minimumDifference(int[] nums, int k) {

        Arrays.sort(nums);
        int min = Integer.MAX_VALUE;
        for(int i=0,j=k-1;j<nums.length;i++,j++){
            min = Math.min(min,nums[j]-nums[i]);
        }
        return min;

    }


    //a, e, i, o, u
    public int maxVowels(String s, int k) {

        int max = 0;
        HashSet<Character> set = new HashSet<>();
        set.add('a');set.add('e');set.add('i');set.add('o');set.add('u');

        int num = 0;
        for(int index=0;index<k;index++){
            if(set.contains(s.charAt(index))){
                num += 1;
            }
        }

        max = Math.max(max,num);
        if(set.contains(s.charAt(0))){
            num -= 1;
        }

        for(int i=1,j=k;j<s.length();i++,j++){
            if(set.contains(s.charAt(j))){
                 num += 1;
            }
            max = Math.max(max,num);
            if(set.contains(s.charAt(i))){
                num -= 1;
            }
        }
        return max;

    }


    public boolean checkInclusion(String s1, String s2) {
        if(s1.length()>s2.length()){
            return false;
        }

        HashMap<Character,Integer> map = new HashMap<>();
        int k = s1.length();
        for(int i=0;i<s1.length();i++){
            int num = map.getOrDefault(s1.charAt(i),0);
            map.put(s1.charAt(i),num+1);
        }

        for(int i=0;i<s1.length()-1;i++){
            if(map.containsKey(s2.charAt(i))){
                int num = map.get(s1.charAt(i));
                map.put(s1.charAt(i),num-1);
            }
        }

        for(int j=s1.length();j<s2.length();j++){
            if(map.containsKey(s2.charAt(j))){
                int num = map.get(s2.charAt(j));
                map.put(s1.charAt(j),num-1);
            }
            if(del(map)){
                return true;
            }
            if(map.containsKey(s2.charAt(j-k+1))){
                int num = map.get(s2.charAt(j-k+1));
                map.put(s1.charAt(j-k+1),num+1);
            }

        }

        return false;

    }

    public boolean del(HashMap<Character,Integer> map){
        for(Character ch:map.keySet()){
            if(map.get(ch)>0){
                return false;
            }
        }
        return true;
    }


    public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {

        int sum = 0;
        int max = 0;
        for(int i=0;i<customers.length;i++){
            if(grumpy[i]==0){
                sum += customers[i];
            }
        }
        max = Math.max(max,sum);
        for(int i=0;i<minutes-1;i++){
            if(grumpy[i]==1){
                sum += customers[i];
            }
        }
        max = Math.max(max,sum);
        for(int i=minutes-1;i<customers.length;i++){
            if(grumpy[i]==1){
                sum += customers[i];
            }
            max = Math.max(max,sum);
            if(grumpy[i-minutes+1]==1){
                sum -= customers[i-minutes+1];
            }
        }

        return max;



    }










}
